<template>
  <el-dialog v-loading="loading" :visible.sync="visible" width="70%" title="选择代码生成模板" :close-on-click-modal="false" :close-on-press-escape="false">
    <vxe-table ref="xTable"
               :loading="dataListLoading"
               class="vxe-table-element"
               max-height="400"
               auto-resize
               @checkbox-change="dataListSelectionChangeHandleVex"
               @checkbox-all="dataListSelectionAllChangeHandleVex"
               :data="dataList">
      <vxe-table-column type="checkbox" width="60" align="center"></vxe-table-column>
      <vxe-table-column field="templateCode" show-overflow title="genTableTemplate.templateCode" sortable width="140"></vxe-table-column>
      <vxe-table-column field="templateName" show-overflow title="genTableTemplate.templateName" sortable width="180"></vxe-table-column>
      <vxe-table-column field="fileName" show-overflow title="genTableTemplate.fileName"></vxe-table-column>
      <vxe-table-column field="appType" show-overflow title="genTableTemplate.appType" width="120">
        <template slot-scope="scope">
          {{$getDictLabel('GEN_TEMP_APP_TYPE', scope.row.appType)}}
        </template>
      </vxe-table-column>
    </vxe-table>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="confirmTemplate()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>
<script>
import mixinViewModule from '@/mixins/view-module'
export default {
  mixins: [mixinViewModule],
  data () {
    return {
      visible: false,
      loading: false,
      dataList: [],
      dataListSelections: [],
      templateType: ''
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.getDataList()
      })
    },
    // 多选
    dataListSelectionChangeHandle (val) {
      this.dataListSelections = val
    },
    getDataList () {
      this.loading = true
      let url = ''
      if (this.templateType) {
        url = `/gen/genTableTemplate/listBytemplateType?templateType=${this.templateType}`
      } else {
        url = `/gen/genTableTemplate/list`
      }
      this.$http.get(url).then(({ data: res }) => {
        this.loading = false
        this.dataList = []
        if (res.code !== 0) {
          this.dataList = []
          return
        }
        this.dataList = res.data
        if (res.data.length > 0) {
          this.$refs.xTable.toggleAllCheckboxRow()
        }
      })
    },
    confirmTemplate () {
      if (this.dataListSelections.length <= 0) {
        return this.$message({
          message: '请选择要生成的代码模板文件',
          type: 'warning',
          duration: 500
        })
      }
      this.$emit('setTemplateList', this.dataListSelections)
      this.visible = false
    }
  }
}
</script>
